<template>
  <div class="home">
    <el-container class="el-container">
      <el-aside width="auto" class="el-aside">
         <div class="logo" @click="toIndex"></div>
        <el-menu
          default-active="2"
          router
          :collapse="iscollapse"
          class="el-menu-admin"
          @open="handleOpen"
          @close="handleClose"
        >
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>用户管理</span>
            </template>
            <el-menu-item index="/user">
            <i class="el-icon-menu"></i>
            <span slot="title">用户列表</span>
            </el-menu-item>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>权限管理</span>
            </template>
            <el-menu-item index="/roles">
            <i class="el-icon-menu"></i>
            <span slot="title">角色列表</span>
            </el-menu-item>
            <el-menu-item index="/rights">
            <i class="el-icon-menu"></i>
            <span slot="title">权限列表</span>
            </el-menu-item>
          </el-submenu>
          <el-submenu index="3">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>商品管理</span>
            </template>
            <el-menu-item index="/goods">
            <i class="el-icon-menu"></i>
            <span slot="title">商品列表</span>
            </el-menu-item>
            <el-menu-item index="/params">
            <i class="el-icon-menu"></i>
            <span slot="title">分类参数</span>
            </el-menu-item>
            <el-menu-item index="/cate">
            <i class="el-icon-menu"></i>
            <span slot="title">商品分类</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container class="el-container">
        <el-header class="el-header">
           <i class="myicon myicon-menu toggle-btn" @click="toggle"></i>
           <div class="system-title">商品后台管理系统</div>
           <div>
              <span class="welcome">您好，{{ username }}</span>
              <el-button type="warning" round @click="quitLogin">退出</el-button>
           </div>
        </el-header>
        <el-main>
           <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<style lang="scss" scoped>
  .home {
    height: 100%;
    .el-menu-admin:not(.el-menu--collapse) {
      width: 200px;
      min-height: 400px;
    }
    .el-container {
      height: 100%;
    }
    .el-aside {
      background-color: #f9f9f9;
    }
    .el-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: #409eff;
    }
    .logo {
      height: 60px;
      background: url(../assets/logo.jpg);
      background-size: cover;
      background-color: white;
    }
    .toggle-btn {
      padding: 0 15px;
      margin-left: -20px;
      font-size: 36px;
      color: white;
      cursor: pointer;
      line-height: 60px;
      &:hover {
        background-color: #4292cf;
      }
    }
    .system-title {
      font-size: 28px;
      color: white;
    }
    .welcome {
      color: white;
      margin-right: 10px;
    }
  }
</style>
<script>
export default {
  data () {
    return {
      username: '',
      iscollapse: false
    }
  },
  methods: {
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    quitLogin () {
      // 清除session,跳转到登录页
      this.$confirm('是否要注销当前账户', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '成功注销!'
        })
        setTimeout(() => {
          localStorage.clear()
          this.$router.push({ name: 'login' })
        }, 1500)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消注销'
        })
      })
    },
    toggle () {
      this.iscollapse = !this.iscollapse
    },
    toIndex () {
      this.$router.push({ name: 'welcome' })
    }
  },
  mounted () {
    // 获取登录的用户信息
    this.username = localStorage.getItem('username')
  }
}
</script>
